﻿
using System.Configuration;

namespace DeskNote
{
    /// <summary>
    /// Class to Get set setting in the app.config file
    /// </summary>
    internal static class Settings
    {
        static private string _todoPath;
        static private int _xAxis = 100;
        static private int _yAxis = 100;
        static private string _originalWall = "";
        static private int _fontSize = 12;
        static private Configuration config;
        /// <summary>
        /// Static constructor, validates config file and updates it.
        /// </summary>
        static Settings()
        {
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings.Count <= 4)
            {
                config.AppSettings.Settings.Clear();
                config.AppSettings.Settings.Add("TODO_Path", "");
                config.AppSettings.Settings.Add("X-CoOrdinates", "100");
                config.AppSettings.Settings.Add("Y-CoOrdinates", "100");
                config.AppSettings.Settings.Add("OriginalWallPaper", "");
                config.AppSettings.Settings.Add("FontSize", "12");
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSetting");
            }
        }
        /// <summary>
        /// Property to get set Todo file path
        /// </summary>
        static internal string ToDoPath
        {
            get
            {
                GetToDoPath();
                return _todoPath;
            }
            set
            {
                _todoPath = value;
                SetToDoPath();
            }
        }
        /// <summary>
        /// Sets todo file path in config file
        /// </summary>
        static private void SetToDoPath()
        {
            config.AppSettings.Settings["TODO_Path"].Value = _todoPath;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSetting");
        }
        /// <summary>
        /// Gets todo file path
        /// </summary>
        static private void GetToDoPath()
        {
            _todoPath = config.AppSettings.Settings["TODO_Path"].Value.ToString();
        }
        /// <summary>
        /// Property to Get/Set XCoord of the text
        /// </summary>
        static internal int X_Axis
        {
            get
            {
                config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                GetXAxis();
                return _xAxis;
            }
            set
            {
                _xAxis = value;
                SetXAxis();
            }
        }
        /// <summary>
        /// Sets x-coord in config file
        /// </summary>
        static private void SetXAxis()
        {
            config.AppSettings.Settings["X-CoOrdinates"].Value = _xAxis.ToString();
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSetting");
        }
        /// <summary>
        /// Gets x-coord from config file
        /// </summary>
        static private void GetXAxis()
        { //DateTime.TryParse(ConfigurationManager.AppSettings["CloseTime"].ToString(), out _closeTime);
            int.TryParse(config.AppSettings.Settings["X-CoOrdinates"].Value.ToString(), out _xAxis);
        }
        /// <summary>
        /// Property to Get/Set YCoord of the text
        /// </summary>
        static internal int Y_Axis
        {
            get
            {
                config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                GetYAxis();
                return _yAxis;
            }
            set
            {
                _yAxis = value;
                SetYAxis();
            }
        }
        /// <summary>
        /// Sets ycoord in config file
        /// </summary>
        static private void SetYAxis()
        {
            config.AppSettings.Settings["Y-CoOrdinates"].Value = _yAxis.ToString();
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSetting");
        }
        /// <summary>
        /// Gets ycoord in config file
        /// </summary>
        static private void GetYAxis()
        {
            int.TryParse(config.AppSettings.Settings["Y-CoOrdinates"].Value.ToString(), out _yAxis);
        }
        /// <summary>
        /// Property to Get/Set original wallpaper location
        /// </summary>
        static internal string OriginalWall
        {
            get
            {
                GetOriginalWall();
                return _originalWall;
            }
            set
            {
                _originalWall = value;
                SetOriginalWall();
            }
        }
        /// <summary>
        /// Sets location of original file as backup in config file
        /// </summary>
        static private void SetOriginalWall()
        {
            config.AppSettings.Settings["OriginalWallPaper"].Value = _originalWall;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSetting");
        }
        /// <summary>
        /// Gets location of the original wallpaper
        /// </summary>
        static private void GetOriginalWall()
        {
            _originalWall = config.AppSettings.Settings["OriginalWallPaper"].Value.ToString();
        }
        /// <summary>
        /// Property to Get/Set fontsize of the text from config
        /// </summary>
        static internal int FontSize
        {
            get
            {
                config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                GetFontSize();
                return _fontSize;
            }
            set
            {
                _fontSize = value;
                SetFontSize();
            }
        }
        /// <summary>
        /// Sets font size in config file
        /// </summary>
        static private void SetFontSize()
        {
            config.AppSettings.Settings["FontSize"].Value = _fontSize.ToString();
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSetting");
        }
        /// <summary>
        /// Gets font size from config file
        /// </summary>
        static private void GetFontSize()
        {
            int.TryParse(config.AppSettings.Settings["FontSize"].Value.ToString(), out _fontSize);
        }
    }

}
